using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using CodRevit.ViewModels;
using CodRevit.Views;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodRevit.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class AddParameterCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                // 获取当前文档和界面应用程序
                UIApplication uiapp = commandData.Application;
                UIDocument uidoc = uiapp.ActiveUIDocument;
                Document doc = uidoc.Document;

                // 检查是否为项目文档
                if (doc.IsFamilyDocument)
                {
                    TaskDialog.Show("错误", "此命令只能在项目文档中使用。");
                    return Result.Failed;
                }

                // 选择族实例
                ICollection<ElementId> selectedIds;
                try
                {
                    selectedIds = uidoc.Selection.PickObjects(
                        ObjectType.Element,
                        new FamilyInstanceSelectionFilter(),
                        "请选择族实例").Select(r => r.ElementId).ToList();
                }
                catch (Autodesk.Revit.Exceptions.OperationCanceledException)
                {
                    return Result.Cancelled;
                }

                if (selectedIds.Count == 0)
                {
                    TaskDialog.Show("提示", "未选择任何族实例。");
                    return Result.Cancelled;
                }

                // 创建ExternalEvent和Handler
                var parameterHandler = new AddParameterHandler();
                var externalEvent = ExternalEvent.Create(parameterHandler);

                // 创建窗口和ViewModel
                var window = new AddParameterView();
                var viewModel = new AddParameterViewModel(
                    uiapp,
                    window,
                    externalEvent,
                    parameterHandler,
                    selectedIds);

                window.DataContext = viewModel;

                var positionedWindow = App.LeftTopWindow(commandData, window);
                if (positionedWindow == null || positionedWindow is not AddParameterView)
                {
                    return Result.Failed;
                }
                window = (AddParameterView)positionedWindow;
                window.Show();
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", $"执行命令时发生错误：{ex.Message}");
                return Result.Failed;
            }
        }
    }

    /// <summary>
    /// 族实例选择过滤器
    /// </summary>
    public class FamilyInstanceSelectionFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            return elem is FamilyInstance;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return false;
        }
    }
}